/*
 * CountDataFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.SQLException;
import java.util.List;

import com.insigma.yanwenlong.bookkeeping.pojo.Account;
import com.insigma.yanwenlong.bookkeeping.pojo.Category;
import com.insigma.yanwenlong.bookkeeping.pojo.vo.DetailVo;
import com.insigma.yanwenlong.bookkeeping.service.AccountService;
import com.insigma.yanwenlong.bookkeeping.service.AccountServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.CategoryService;
import com.insigma.yanwenlong.bookkeeping.service.CategoryServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.DetailService;
import com.insigma.yanwenlong.bookkeeping.service.DetailServiceImpl;

/**
 *
 * @author  __USER__
 */
public class CountDataFrame extends javax.swing.JFrame {
	private AccountService accountService = new AccountServiceImpl();
	private DetailService detailService = new DetailServiceImpl();
	private CategoryService categoryService = new CategoryServiceImpl();

	/** Creates new form CountDataFrame */
	public CountDataFrame() {
		this.setTitle("统计分析");
		initComponents();
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		//初始化数据
		initCountData();
	}

	private void initCountData() {
		//总初始金
		double initAccount = 0;
		try {
			List<Account> accounts = accountService.findaAccounts();
			for (Account account : accounts) {
				initAccount += account.getInitAccount();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//收入总额和支出总额
		double income = 0;
		double outcome = 0;
		try {
			List<DetailVo> details = detailService.findDetails();
			Category category = null;
			for (DetailVo detailVo : details) {
				category = categoryService.findDirectionIdById(detailVo
						.getCategoryId());
				if (category.getDirectionId() == 1) {
					outcome += detailVo.getHappenMoney();
				} else {
					income += detailVo.getHappenMoney();
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//净资产额
		double total = income - outcome;

		lblInitAccount.setText(String.valueOf(initAccount));
		lblIncome.setText(String.valueOf(income));
		lblOutcome.setText(String.valueOf(outcome));
		lblTotal.setText(String.valueOf(total));
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		lblInitAccount = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		lblIncome = new javax.swing.JLabel();
		lblOutcome = new javax.swing.JLabel();
		lblTotal = new javax.swing.JLabel();
		btnDayConsume = new javax.swing.JButton();
		btnYearConsume = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("\u603b\u521d\u59cb\u91d1\uff1a");

		jLabel2.setText("\u6536\u5165\u603b\u989d\uff1a");

		jLabel3.setText("\u652f\u51fa\u603b\u989d\uff1a");

		jLabel4.setText("\u51c0\u8d44\u4ea7\u989d\uff1a");

		btnDayConsume.setText("\u65e5\u5e38\u6536\u652f\u8868");
		btnDayConsume.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnDayConsumeActionPerformed(evt);
			}
		});

		btnYearConsume.setText("\u5e74\u5ea6\u6536\u652f\u8868");
		btnYearConsume.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnYearConsumeActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				51,
																				51,
																				51)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addComponent(
																								jLabel2,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								jLabel3,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								jLabel4,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								jLabel1,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				btnDayConsume)))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																btnYearConsume)
														.addGroup(
																layout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING,
																				false)
																		.addComponent(
																				lblIncome,
																				javax.swing.GroupLayout.Alignment.LEADING,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				lblInitAccount,
																				javax.swing.GroupLayout.Alignment.LEADING,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				100,
																				Short.MAX_VALUE)
																		.addComponent(
																				lblOutcome,
																				javax.swing.GroupLayout.Alignment.LEADING,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				100,
																				Short.MAX_VALUE))
														.addComponent(
																lblTotal,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																100,
																Short.MAX_VALUE))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(33, 33, 33)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(
																lblInitAccount,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																15,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel1))
										.addGap(33, 33, 33)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																lblIncome,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																15,
																Short.MAX_VALUE)
														.addComponent(jLabel2))
										.addGap(30, 30, 30)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel3)
														.addComponent(
																lblOutcome,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																15,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(33, 33, 33)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(jLabel4)
														.addComponent(
																lblTotal,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																15,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												55, Short.MAX_VALUE)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																btnDayConsume)
														.addComponent(
																btnYearConsume))
										.addGap(33, 33, 33)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	// 点击年度收支
	private void btnYearConsumeActionPerformed(java.awt.event.ActionEvent evt) {
		// 显示年度收支界面
		new YearConsumeFrame().setVisible(true);
	}

	// 点击日常收支
	private void btnDayConsumeActionPerformed(java.awt.event.ActionEvent evt) {
		// 显示日常收支界面
		new DayConsumeFrame().setVisible(true);
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new CountDataFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnDayConsume;
	private javax.swing.JButton btnYearConsume;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel lblIncome;
	private javax.swing.JLabel lblInitAccount;
	private javax.swing.JLabel lblOutcome;
	private javax.swing.JLabel lblTotal;
	// End of variables declaration//GEN-END:variables

}